class Solution:
    def reachableNodes(self, n: int, edges: list, restricted: list) -> int:
        adj = [[] for _ in range(n)]
        for edge in edges:
            adj[edge[0]] += [edge[1]]
            adj[edge[1]] += [edge[0]]
        visited = set()
        visited.add(0)
        queue = [0]
        res = 0
        restrictedSet = set(restricted)
        # 广度优先搜索，遇到不被访问，被访问过的节点就跳过，计数访问到的节点，得到的便是结果
        while queue:
            n = queue.pop(0)
            res += 1
            for nextN in adj[n]:
                if nextN in visited:
                    continue
                if nextN in restrictedSet:
                    continue
                queue.append(nextN)
                visited.add(nextN)

        return res
